#ifndef TOYBRICKS_H
#define TOYBRICKS_H

#include <QBitArray>
#include <QColor>
#include <QVector>
#include <QTime>
#include <QTimer>
#include <QPoint>
#include <QPainter>


enum MoveDirection{
    Left,
    Right,
    Down,
    Here
};

namespace Ui {
    class ToyBricks;
}

class ToyBricks
{
public:
    ToyBricks();
    ToyBricks(quint8 rows,quint8 cols);
    ToyBricks(quint8 [],quint8 rows,quint8 cols,Qt::GlobalColor color);
    //ToyBricks(const ToyBricks &piece);

    QColor  color(quint8 idx) const {return _data[idx] > 0 ? _data[idx] : quint8(Qt::white);}
    void    setColor(quint8 idx,Qt::GlobalColor color);
    void    setColor(quint8 row,quint8 col,Qt::GlobalColor color);
    void    setColor(Qt::GlobalColor color);

    quint8 width() const {return _width;}
    quint8 height() const {return _width > 0 ? _data.size() / _width : 0;}
    quint32 size()  const {return _data.size();}
    quint8 getValue(quint32 idx) const {return idx < size() ? _data[idx] : 0;}
    void   setValue(quint32 idx,quint8 val) { if(idx < size()) _data[idx] = val; }
    bool   isFilled(quint8 row,quint8 col) const {return getValue(row * _width + col) > 0;}

    quint8  getRowFilledCnt(quint8 row) const {return row < height() ? _fill[row] : 0;}
    void    setRowFilledCnt(quint8 row,quint8 val) {if(row < height()) _fill[row] = val;}

    void    clean();
    void    rotateLeft();
    bool    isCollision(const ToyBricks &piece,const QPoint &point);
    void    join(const ToyBricks &piece,const QPoint &point);
    void    draw(QPainter &painter,const QPoint &pos,quint8 cell_size);

    static ToyBricks createTetrixPiece();
private:    
    quint8          _width;
    QVector<quint8> _data;
    QVector<quint8> _fill;
};

#endif // TOYBRICKS_H
